package Service;



import QQcommon.Massage_2;
import QQcommon.Message;
import view.Utility;


import java.io.ObjectOutputStream;


import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

public class Service_SendNews implements Runnable {


    @Override
    public void run() {
        while (true) {
            System.out.println("输入你要推送的新闻：/输入0关闭该功能");
            String news = Utility.readString(100);
            if (news.equals("0")) {
                break;
            }
            Message message = new Message();
            message.setContent(news);
            message.setSender("服务器");
            message.setMassageType(Massage_2.MESSAGE_TO_ALL_MES);
            message.setSendTime(new Date().toString());
            System.out.println("服务器向所有人推送：" + news);

            HashMap<String, Service_Thread> hm = Massage_Thread.getHm();
            Iterator<String> iterator = hm.keySet().iterator();

            while (iterator.hasNext()) {
                String olineUser = iterator.next().toString();
                try {
                    ObjectOutputStream oos=new ObjectOutputStream(hm.get(olineUser).getSocket().getOutputStream());
                    oos.writeObject(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
